﻿using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Partner.Surround.Controllers;
using Partner.Surround.Editions;
using Partner.Surround.Editions.Dto;
using Partner.Surround.MultiTenancy;
using Partner.Surround.Web.Models.Common;
using Partner.Surround.Web.Models.Editions;

namespace WuLong.Lims.Web.Controllers
{
    /// <summary>
    /// 版本管理控制器
    /// </summary>
    [AbpMvcAuthorize]
    public class EditionsController : SurroundControllerBase
    {
        #region 初始化
        private readonly IEditionAppService _editionAppService;
        private readonly TenantManager _tenantManager;

        public EditionsController(
            IEditionAppService editionAppService,
            TenantManager tenantManager)
        {
            _editionAppService = editionAppService;
            _tenantManager = tenantManager;
        }
        #endregion

        #region 版本管理
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取版本数据列表
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetEditionsList()
        {
            var editionListDto = await _editionAppService.GetEditionsList();
            return Json(new ResponseParamListViewModel<EditionListDto>(editionListDto.Items));
        }

        /// <summary>
        /// 新增或更新版本页面
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> CreateOrUpdateEdition(NullableIdDto input)
        {
            var output = await _editionAppService.GetEditionForEdit(input);

            var featureList = ObjectMapper.Map<List<FeatureListViewModel>>(output.Features);

            foreach (var feature in featureList)
            {
                foreach (var featureValue in output.FeatureValues)
                {
                    if (featureValue.Name == feature.Name)
                    {
                        feature.Checked = Convert.ToBoolean(featureValue.Value);
                        break;
                    }
                }
            }

            var editionFormViewModel = new EditionFormViewModel()
            {
                Edition = output.Edition,
                Features = featureList
            };

            return View(editionFormViewModel);
        }

        /// <summary>
        /// 新增或更新版本
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> CreateOrUpdateEdition([FromBody]CreateOrUpdateEditionInput input)
        {
            await _editionAppService.CreateOrUpdateEdition(input);
            return Json(new ResponseParamViewModel(input.Edition.Id.HasValue ? L("更新版本成功") : L("添加版本成功")));
        }

        /// <summary>
        /// 删除版本
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> DeleteEdition([FromBody]EntityDto input)
        {
            await _editionAppService.DeleteEdition(input);

            return Json(new ResponseParamViewModel(L("删除版本成功")));
        }
        #endregion
    }
}
